package com.dycong.common.IO.netty.frame_coder;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.logging.LoggingHandler;
import org.apache.log4j.Logger;

/**
 * Created by dycong on 2017/3/13.
 */
@ChannelHandler.Sharable        //@Sharable 标识这类的实例之间可以在 channel 里面共享
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    static final Logger log = Logger.getLogger(EchoServerHandler.class);
    static int count = 0;
    LoggingHandler loggingHandler = new LoggingHandler(EchoServerHandler.class);

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(("server!" + (++count) + Thread.currentThread()));
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Msg msg1 = (Msg) msg;
        System.out.println("accept: " + msg1.toString() + ":" + Thread.currentThread());
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("sssssssssssssssssssssssssss");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}
